package ecs.jsp;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import com.swf.seed.base.SdCharset;
import com.swf.seed.base.SystemContants;
import com.swf.seed.regex.PatternUtils;
import com.swf.seed.triutil.lang.SdFileUtils;

public class JspCreate {
	public static String labelTemplate="<tr><td class=\"${required}\">${desc}(${name})</td><td><input  name=\"${name}\" value=\"\" /></td></tr>";
	public static String labelTemplate1="<br><label>${desc}(${name})</label><input  name=\"${name}\" value=\"\" />";
	public static String regex="(\\S+)\\s+(\\S+)\\s+(\\S+)";
	public static void main(String[] args) throws Exception {
		File file = SdFileUtils.getFile("paf.txt");
		if (!file.exists()){
			System.out.println("文件不存在："+file.getAbsolutePath());
			return;
		}
		StringBuffer sb = new StringBuffer();
		List<Label> labelList = doContent(file);
		for (Label label:labelList){
			sb.append(labelTemplate.replace("${name}", label.getName()).replace("${desc}", label.getDesc()).replace("${required}", label.getRequired()));
			sb.append(SystemContants.LINE_SEPARATOR);
		}
		System.out.println(sb.toString());
	}

	private static List<Label> doContent(File file) throws FileNotFoundException {
		Label label;
		List<Label> labelList = new ArrayList<Label>();
		String content = SdFileUtils.loadFile(file, SdCharset.UTF_8);
		content = dealContent(content);
			List<List<String>> allList = PatternUtils.matchedList(content, regex, new int[]{1,2,3});
			for (List<String> l:allList){
				//System.out.println(l);
				labelList.add(new Label(l.get(1),l.get(0),"是".equals(l.get(2))?"required":"notRequired"));
			}
			return labelList;
	}

	private static String dealContent(String content) {
		List<String> list = PatternUtils.matchedList(content, ".+");
		StringBuffer sb = new StringBuffer();
		for (String s:list){
			sb.append(s).append("\n");
		}
		return sb.toString();
	}
	
	
}
